package com.hb.trade.server.project.file.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hb.core.pojo.enums.FirstReviewItemTypeEnum;
import com.hb.core.pojo.project.evaluation.entity.EvaluationCommitteeMembers;
import com.hb.core.pojo.project.file.dto.ReviewEvaluationFormDTO;
import com.hb.core.pojo.project.file.dto.ReviewItemFirstDto;
import com.hb.core.pojo.project.file.dto.ReviewItemSecondDto;
import com.hb.core.pojo.project.file.entity.ReviewEvaluationForm;
import com.hb.core.pojo.project.section.entity.BidSection;
import com.hb.trade.server.project.evaluation.service.EvaluationCommitteeMembersService;
import com.hb.trade.server.project.file.service.ReviewItemFirstService;
import com.hb.trade.server.project.file.service.ReviewItemSecondService;
import com.hb.trade.server.project.section.service.raw.BidSectionService;
import org.fzzn.core.model.enums.LogicEnum;
import org.fzzn.core.util.Enums;

import java.util.List;

import static com.hb.core.pojo.enums.FirstReviewItemTypeEnum.*;

/**
 * describe:
 * 生成表格的
 * @author zhongdl
 * @date 2022/04/19
 */
public class ReviewEvaluationFormManager extends ReviewEvaluationFormDTO {



    public List<ReviewEvaluationForm> generateReviewEvaluationForm(){
        List<ReviewEvaluationForm> list = CollUtil.list(false);
        Long sectionId = this.getSectionId();
        Long fileId = this.getFileId();
        if (fileId == null){
           final BidSectionService bidSectionService = SpringUtil.getBean(BidSectionService.class);
           BidSection bidSection = bidSectionService.getById(sectionId);
           fileId = bidSection.getFileId();
        }
        //初步评审
        List<ReviewEvaluationForm> preliminaryList = generateItem(sectionId, fileId, PRELIMINARY_REVIEW);
        //详细评审
        List<ReviewEvaluationForm> detailedList = generateItem(sectionId, fileId, DETAILED_REVIEW);
        //报价评审
        List<ReviewEvaluationForm> quotationList = generateItem(sectionId, fileId, QUOTATION_REVIEW);
        List<ReviewEvaluationForm> total = CollUtil.list(false);
        if (CollUtil.isNotEmpty(quotationList) || CollUtil.isNotEmpty(detailedList)){
            //评分汇总
            total  = generateItem(sectionId, fileId, OTHER);
        }

        CollUtil.addAll(list,preliminaryList);
        CollUtil.addAll(list,detailedList);
        CollUtil.addAll(list,quotationList);
        CollUtil.addAll(list,total);
        if (CollUtil.isEmpty(list)){
            return list;
        }
        int size = list.size();
        for (int i = 0; i < size; i++) {
            ReviewEvaluationForm reviewEvaluationForm = list.get(i);
            reviewEvaluationForm.setSort(i + 1);
        }
        return list;

    }


    public static List<ReviewEvaluationForm> generateItem(Long sectionId, Long fileId, FirstReviewItemTypeEnum reviewItemTypeEnum){
        List<ReviewEvaluationForm> list = CollUtil.list(false);
        ReviewItemFirstService firstService = SpringUtil.getBean(ReviewItemFirstService.class);
        ReviewItemSecondService reviewItemSecondService = SpringUtil.getBean(ReviewItemSecondService.class);
        //查询第一评审项
        ReviewItemFirstDto reviewItemFirst = firstService.getReviewItemFirst(fileId, Enums.equals(OTHER,reviewItemTypeEnum.getCode()) ? PRELIMINARY_REVIEW.getCode() : reviewItemTypeEnum.getCode());
        if (reviewItemFirst == null){
            return list;
        }

        Long basicsFirstId = reviewItemFirst.getId();//招标文件评审表一级评审ID
        //查询二级评审项
        List<ReviewItemSecondDto> reviewItemSeconds = reviewItemSecondService.selectReviewItemSecondList(basicsFirstId);
        switch (reviewItemTypeEnum){
            //初步评审
            case PRELIMINARY_REVIEW:
                for (ReviewItemSecondDto reviewItemSecond : reviewItemSeconds) {
                    ReviewEvaluationForm reviewEvaluationForm = new ReviewEvaluationForm(sectionId,
                            fileId,
                            basicsFirstId,
                            reviewItemSecond.getId(),
                            PRELIMINARY_REVIEW.getCode(),
                            reviewItemSecond.getSecondReviewItemName(),
                            2,
                            -1L,
                            "全体专家",
                            LogicEnum.NO.getCode());
                    reviewEvaluationForm.buildBaseData(1);
                    list.add(reviewEvaluationForm);
                }
                break;
            case DETAILED_REVIEW:
                //详细评审个人
                EvaluationCommitteeMembersService evaluationCommitteeMembersService = SpringUtil.getBean(EvaluationCommitteeMembersService.class);
                List<EvaluationCommitteeMembers> evaluationCommitteeMembers = evaluationCommitteeMembersService.findBySectionId(sectionId);
                for (EvaluationCommitteeMembers evaluationCommitteeMember : evaluationCommitteeMembers) {
                    String memberName = evaluationCommitteeMember.getMemberName();
                    Long memberId = evaluationCommitteeMember.getMemberId();
                    for (ReviewItemSecondDto reviewItemSecond : reviewItemSeconds) {
                        ReviewEvaluationForm reviewEvaluationForm = new ReviewEvaluationForm(sectionId,
                                fileId,
                                basicsFirstId,
                                reviewItemSecond.getId(),
                                DETAILED_REVIEW.getCode(),
                                reviewItemSecond.getSecondReviewItemName(),
                                1,
                                memberId,
                                memberName,
                                LogicEnum.NO.getCode());
                        reviewEvaluationForm.buildBaseData(1);
                        list.add(reviewEvaluationForm);
                    }
                }
                //详细评审总的
                for (ReviewItemSecondDto reviewItemSecond : reviewItemSeconds) {
                    ReviewEvaluationForm reviewEvaluationForm = new ReviewEvaluationForm(sectionId,
                            fileId,
                            basicsFirstId,
                            reviewItemSecond.getId(),
                            DETAILED_REVIEW.getCode(),
                            reviewItemSecond.getSecondReviewItemName(),
                            2,
                            -1L,
                            "全体专家",
                            LogicEnum.NO.getCode());
                    reviewEvaluationForm.buildBaseData(1);
                    list.add(reviewEvaluationForm);
                }
                break;
             //评标价算法
            case QUOTATION_REVIEW:
                    ReviewEvaluationForm reviewScoreEvaluationForm = new ReviewEvaluationForm(sectionId,
                            fileId,
                            basicsFirstId,
                            -1L,
                            QUOTATION_REVIEW.getCode(),
                             "报价评审",
                            2,
                            -1L,
                            "全体专家",
                            LogicEnum.NO.getCode());

                reviewScoreEvaluationForm.buildBaseData(1);
                    list.add(reviewScoreEvaluationForm);

                break;
            default:
                //评分汇总
                ReviewEvaluationForm reviewEvaluationForm = new ReviewEvaluationForm(sectionId,
                                                                                     fileId,
                                                                                     -1L,
                                                                                     -1L,
                                                                                     -1,
                                                                                     "评分汇总",
                                                                                     2,
                                                                                     -1L,
                                                                                     "全体专家",
                                                                                     LogicEnum.NO.getCode());
                reviewEvaluationForm.buildBaseData(1);
                list.add(reviewEvaluationForm);
                //评标报告
                ReviewEvaluationForm reviewReportEvaluationForm = new ReviewEvaluationForm(sectionId,
                        fileId,
                        -1L,
                        -1L,
                        0,
                        "评标结论",
                        2,
                        -1L,
                        "全体专家",
                        LogicEnum.NO.getCode());
                reviewReportEvaluationForm.buildBaseData(1);
                list.add(reviewReportEvaluationForm);
                break;

        }
        return list;

    }
}
